﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;

namespace org.psantos.ff.flattner
{
    public class FolderFlattner
    {
        public int FlattenPath(string path, string newPath)
        {
            string newName = "";
            int count = 0;

            if (String.IsNullOrEmpty(path) || !Directory.Exists(path))
                throw new ArgumentNullException("Argument 'path' cannot be null and must point to a valid directory");

            DirectoryInfo pathDirInfo = new DirectoryInfo(path);
            string pathDirName = pathDirInfo.Name;
            string parentDirName = pathDirInfo.Parent.Name;

            string[] albumNameParts = pathDirName.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            string[] artistNameParts = parentDirName.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            string albumAcronym = albumNameParts[0][0] + ((albumNameParts.Length > 1) ? String.Empty + albumNameParts[1][0] : String.Empty) + ((albumNameParts.Length > 2) ? String.Empty + albumNameParts[2][0] : String.Empty);
            string artistAcronym = artistNameParts[0][0] + ((artistNameParts.Length > 1) ? String.Empty + artistNameParts[1][0] : String.Empty) + ((artistNameParts.Length > 2) ? String.Empty + artistNameParts[2].ToCharArray()[0] : String.Empty);

            ArrayList files = new ArrayList();
            FileInfo[] fileInfos = pathDirInfo.GetFiles("*.mp3");
            files.AddRange(fileInfos);
            fileInfos = pathDirInfo.GetFiles("*.wma");
            files.AddRange(fileInfos);

            foreach (FileInfo fi in files)
            {
                //fi.CopyTo(newPath+"\\"+artistAcronym+"_"+albumAcronym+"_"+fi.Name);
                System.Console.Out.WriteLine(newPath + "\\" + artistAcronym + "_" + albumAcronym + "_" + fi.Name);
                count++;
            }

            return count;
        }

        private int CopyFiles(string sourcePath, string destPath)
        {
            int copiedFiles = 0;


            return copiedFiles;
        }

        public ArrayList GetDrivesList()
        {
            ArrayList driveLabels = new ArrayList();
            DriveInfo[] driveInfos = DriveInfo.GetDrives();
            foreach (DriveInfo di in driveInfos)
            {
                if (di.IsReady)
                    driveLabels.Add(di.VolumeLabel + "(" + di.Name + ")" + " - " + di.TotalFreeSpace);
                else
                    driveLabels.Add("(" + di.Name + ")"+"Not ready");
            }
            return driveLabels;
        }
    }
}
